<?php


namespace app\finance\servers;


use think\facade\Db;

class BaseService
{
    /**
     * @autho:xiangzi
     * @auth:true
     * @Date:2024/12/19
     * 获取管理津贴
     */
    public static function getGljt($adminId,$did,$possitionId,$year)
    {


        $xsgcsId=Db::name('admin')->where('status',1)->where('did',$did)
            ->where('id','<>',$adminId)->where('position_id',$possitionId)->column('id');
        $gcsndjx=0;
        $gcsnzjx=0;
        foreach ($xsgcsId as $xi){
            $scList=Db::name('sale_contract')->where('status',4)
                ->where('saleId',$xi)
                ->where('type',1)
                ->where('payStatus',3)
                ->whereYear('payOverTime',$year)
                ->order('payOverTime ASC')
                ->field('id,countMoney,orderNum,payOverTime')->select();
            $dTargets=Db::name('sale_target_detail')
                ->alias('std')
                ->leftJoin('sale_target st','st.id=std.orderId')
                ->where('std.status',2)
                ->where('std.adminId',$xi)
                ->where('st.years',$year)
                ->order('std.create_time desc')
                ->value('dTargets');
            $ordernums=[];
            $overed=0;
            foreach ($scList as $v){
                $ordernums[]=$v['orderNum'];
                $overed+=$v['countMoney'];
            }
            $ndhtl=round($overed/$dTargets,2);
            if ($ndhtl<0.8){
                $ndjx=0;
                $nzjx=0;
            }
            if (0.8<=$ndhtl&&$ndhtl<=1.5){
                $ndjx=round($overed*0.1*0.25,2);
                $cgcb=Db::name('sale_contract_detail')
                    ->where('status',4)->where('type',2)
                    ->where('saleContractOrderNum','in',$ordernums)->sum('bjMoney');
                $bxjeId=Db::name('expense')->where('admin_id',$adminId)->where('status',5)
                    ->where('contractOrder','in',$ordernums)->column('id');
                $bxje=Db::name('expense_interfix')->where('orderId','in',$bxjeId)->sum('amount');
                $nzjx=(($cgcb/$overed)/1.13-$bxje)*0.08;
                $nzjx=round($nzjx,2);
                $gcsndjx+=$ndjx;
                $gcsnzjx+=$nzjx;
            }
            if ($ndhtl>1.5){
                $ndjx=round($dTargets*1.5*0.01*0.25,2);
                $nzjx=round($dTargets*1.5*0.11*0.08,2);
                $gcsndjx+=$ndjx;
                $gcsnzjx+=$nzjx;
            }

        }
        $gljx=($gcsndjx+$gcsnzjx)*0.12;

        return round($gljx,2);

}
}